package com.glsc.ngateway.common.api.platform.resp;

import com.glsc.ngateway.common.api.platform.enums.PlatformStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Map;

/**
 * 新版本返回，主要是支持泛型，方便前段熟悉接口报文
 * @author zzp
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("platform通用返回体")
public class PlatformResponse<T> {

    @ApiModelProperty(value = "状态码")
    public int status;

    @ApiModelProperty(value = "具体状态信息")
    public String msg;

    @ApiModelProperty(value = "具体错误信息，不展示")
    public String errmsg;

    @ApiModelProperty(value = "fileId")
    public String fileId;

    @ApiModelProperty(value = "head信息")
    public Map<Integer, String> head;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public PlatformResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public static PlatformResponse<?> success() {
        PlatformResponse<Object> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Success.getErrorCode());
        response.setMsg("操作成功");
        response.setData(new ArrayList<>());
        return response;
    }

    /**
     * 直接返回成功信息
     * TODO：有存在将成功数据返回的场景调用此接口，将此类场景改正
     */
    public static <T> PlatformResponse<T> successMsg(String msg) {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Success.getErrorCode());
        response.setMsg(msg);
        return response;
    }

    public static <T> PlatformResponse<T> successData(T data) {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Success.getErrorCode());
        response.setMsg("操作成功");
        response.setData(data);
        return response;
    }

    public static <T> PlatformResponse<T> failed() {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Fail.getErrorCode());
        response.setMsg("操作失败");
        return response;
    }

    public static <T> PlatformResponse<T> failedMsg(String msg) {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Fail.getErrorCode());
        response.setMsg(msg);
        return response;
    }

    public static <T> PlatformResponse<T> failedMsg(String msg, String errmsg) {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Fail.getErrorCode());
        response.setMsg(errmsg);
        response.setErrmsg(errmsg);
        return response;
    }

    public static <T> PlatformResponse<T> failedData(T data) {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setStatus(PlatformStatus.Fail.getErrorCode());
        response.setMsg("操作失败");
        response.setData(data);
        return response;
    }

    public boolean isSuccess(){
        return this.getStatus() == PlatformStatus.Success.getErrorCode();
    }

    public boolean isFailed(){
        return this.getStatus() == PlatformStatus.Fail.getErrorCode();
    }

    @Override
    public String toString() {
        return "PlatformResponse [status=" + status + ", msg=" + msg + ", fileId=" + fileId + ", head=" + head + ", data="
                + data + "]";
    }

}
